package com.codermn.demo9;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

/**
 * @author jxau_scf
 * @date 2021/6/28 - 16:42
 */
public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true){
            Socket socket = serverSocket.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        InputStream is = socket.getInputStream();
                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        String readLine = br.readLine();
                        System.out.println(readLine);
                        String htmlPath = readLine.split(" ")[1].split("/")[2];
                        FileInputStream fis = new FileInputStream(htmlPath);
                        OutputStream os = socket.getOutputStream();
                        os.write("HTTP/1.1 200 OK\r\n".getBytes());
                        os.write("Content-Type:text/html\r\n".getBytes());
                        os.write("\r\n".getBytes());
                        byte[] bytes = new byte[1024];
                        int len = 0;
                        while ((len = fis.read(bytes)) != -1){
                            os.write(bytes, 0, len);
                        }
                        fis.close();
                        socket.close();

                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

//    public static void main(String[] args) throws IOException {
//        ServerSocket serverSocket = new ServerSocket(8888);
//        while (true){
//            Socket socket = serverSocket.accept();
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        InputStream is = socket.getInputStream();
//                        File file = new File("upload");
//                        if (!file.exists()){
//                            file.mkdirs();
//                        }
//                        String fileName = "nd"+System.currentTimeMillis()+new Random().nextInt(888888)+".jpg";
//                        FileOutputStream fos = new FileOutputStream(file + "\\"+fileName);
//
//                        int len = 0;
//                        byte[] bytes = new byte[1024];
//                        while ((len = is.read(bytes)) != -1){
//                            fos.write(bytes, 0, len);
//                        }
//                        socket.getOutputStream().write("上传成功".getBytes());
//                        fos.close();
//                        socket.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
//        }
////        serverSocket.close();
//
//
//
//
//
//    }



//    public static void main(String[] args) throws IOException {
//        ServerSocket serverSocket = new ServerSocket(8888);
//        Socket socket = serverSocket.accept();
//        InputStream is = socket.getInputStream();
//        File file = new File("upload");
//        if (!file.exists()){
//            file.mkdirs();
//        }
//        FileOutputStream fos = new FileOutputStream(file + "\\1.jpg");
//        int len = 0;
//        byte[] bytes = new byte[1024];
//        while ((len = is.read(bytes)) != -1){
//            fos.write(bytes, 0, len);
//        }
//        socket.getOutputStream().write("上传成功".getBytes());
//        fos.close();
//        socket.close();
//        serverSocket.close();
//
//
//    }
}
